﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;
using Common.Components;
using Common.Entities;
using MobileSales.Interface;
using MobileSales.Presenter;
using Visits;

namespace MobileSales
{
    public partial class MainForm : Form, IMainFormView
    {

        #region IMainFormView Members

        public DateTime DateWork
        {
            get
            {
                return taskDayDateTimePicker.Value;
            }
            set
            {
                taskDayDateTimePicker.Value = value;
            }
        }


        public int FormTag
        {
            get
            {
                return (int)Tag;
            }
        }

        public Order CurrentOrder
        {
            get { return (Order)orderBindingSource.Current; }
        }

        public object DataSource
        {
            set
            {
                visitBindingSource.DataSource = value;
            }

            get
            {
                return visitBindingSource.DataSource;
            }
        }

        public object Current
        {
            get
            {
                return visitBindingSource.Current;
            }
        }

        public bool EnableForm 
        {
            set { this.Enabled = value; }
        }

        public bool VizibleForm
        {
            set { this.Visible = value; }
        }

        public  event EventHandler<EventArgs> InitializedComponents;
        public  event EventHandler<EventArgs> LoadedForm;
        public event EventHandler<EventArgs> ChangeDataWork;
        public event EventHandler<EventArgs> SelectGrapVisitAction;
        public event EventHandler<EventArgs> SelectReportAction;
        public event EventHandler<EventArgs> SelectAboutAction;
        public event EventHandler<EventArgs> SelectSettingsAction;
        public event EventHandler<EventArgs> SelectBuildVisitAction;
        public event EventHandler<EventArgs> SelectCheckVisitAction;
        public event EventHandler<EventArgs> SelectCreateOrderAction;
        public event EventHandler<EventArgs> SelectImportDataAction;
        public event EventHandler<EventArgs> SelectExportDataAction;
        public event EventHandler<EventArgs> EditOrderAction;
        public event EventHandler<EventArgs> DeleteOrderAction;

        #endregion

        public MainForm()
        {
            new MainFormPresenter(this);
            InitializeComponent();
            InitializedComponents(null, null);

            DataGridColumnStyle dataColumn;
            DataGridTableStyle ts = new DataGridTableStyle();
            dataColumn = new DataGridCheckBoxColumn();
            dataColumn.HeaderText = "В";
            dataColumn.MappingName = "IsVisit";
            dataColumn.NullText = "null";
            dataColumn.Width = 20;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridCheckBoxColumn();
            dataColumn.HeaderText = "З";
            dataColumn.MappingName = "IsOrder";
            dataColumn.NullText = "null";
            dataColumn.Width = 20;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridTextBoxColumn();
            dataColumn.HeaderText = "Назва торгової точки";
            dataColumn.MappingName = "ByPointName";
            dataColumn.NullText = "";
            dataColumn.Width = 150;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridTextBoxColumn();
            dataColumn.HeaderText = "Адреса";
            dataColumn.MappingName = "BuyPointAdress";
            dataColumn.NullText = "";
            dataColumn.Width = 200;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridTextBoxColumn();
            dataColumn.HeaderText = "Час";
            dataColumn.MappingName = "TimeEndVisit";
            dataColumn.NullText = "";
            dataColumn.Width = 100;
            ts.GridColumnStyles.Add(dataColumn);

            ts.MappingName = "Visit";
            visitsDataGrid.TableStyles.Add(ts);

            ts = new DataGridTableStyle();

            dataColumn = new DataGridCheckBoxColumn();
            dataColumn.HeaderText = "В";
            dataColumn.MappingName = "IsReadOnly";
            dataColumn.NullText = "null";
            dataColumn.Width = 20;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridTextBoxColumn();
            dataColumn.HeaderText = "Номер";
            dataColumn.MappingName = "NumberOrder";
            dataColumn.NullText = "";
            dataColumn.Width = 50;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridTextBoxColumn();
            dataColumn.HeaderText = "Дата відгрузки";
            dataColumn.MappingName = "DateOrder";
            dataColumn.NullText = "";
            dataColumn.Width = 100;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridTextBoxColumn();
            dataColumn.HeaderText = "Сума";
            dataColumn.MappingName = "SumOrder";
            dataColumn.NullText = "";
            dataColumn.Width = 100;
            ts.GridColumnStyles.Add(dataColumn);
            ts.MappingName = "Order";
            visitsDataGrid.TableStyles.Add(ts);

            visitsDataGrid.RowHeadersVisible = false;
            orderBindingSource.DataSource = visitBindingSource;
            orderBindingSource.DataMember = "Orders";
            Tag = 0;

            buyerComboBox.DisplayMember = "ByPointName";
            BuyerPointAdressTextBox.DataBindings.Add("Text", visitBindingSource, "BuyPointAdress");
            orderBindingSource.DataMember = "Orders";

            ChangeView();
        }

        private void taskDayDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                ChangeDataWork(null, null);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }

        private void ChangeView ()
        {
            if ((int)this.Tag == 0)
            {
                selectBindingButton.Text = "Заявки";
                mainOperationMenuItem.Text = "Відмітити";
                visitsDataGrid.DataSource = visitBindingSource;
                this.Tag = 1;
            }
            else
            {
                selectBindingButton.Text = "Візити";
                mainOperationMenuItem.Text = "Створити";
                visitsDataGrid.DataSource = orderBindingSource;
                this.Tag = 0;
            }
        }

        private void SelectBindingButton_Click(object sender, EventArgs e)
        {
            ChangeView();
        }

        private void mainOperationMenuItem_Click(object sender, EventArgs e)
        {
            if ((int)Tag == 1)
                SelectCheckVisitAction(null, null);
            else
                SelectCreateOrderAction(null, null);
            visitsDataGrid.Refresh();
        }

        private void visitBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            if (visitBindingSource.Count == 0)
            {
                mainOperationMenuItem.Enabled = false;
                selectBindingButton.Enabled = false;
            }
            else
            {
                mainOperationMenuItem.Enabled = true;
                selectBindingButton.Enabled = true;
            }
        }

        private void grapVisitMenuItem_Click(object sender, EventArgs e)
        {
            SelectGrapVisitAction(null, null);
        }

        private void aboutMenuItem_Click(object sender, EventArgs e)
        {
            SelectAboutAction(null, null);
        }

        private void settingsMenuItem_Click(object sender, EventArgs e)
        {
            SelectSettingsAction(null, null);
        }

        private void reportMenuItem_Click(object sender, EventArgs e)
        {
            //SelectReportAction(null, null);
        }

        private void buildVisitMenuItem_Click(object sender, EventArgs e)
        {
            SelectBuildVisitAction(null, null);
        }

        private void orderBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            if (((Order)orderBindingSource.Current).IsReadOnly)
            {
                visitsDataGrid.ContextMenu = null;
            }
            else
            {
                visitsDataGrid.ContextMenu = orderContextMenu;
            }
        }

        private void editOrderMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                EditOrderAction(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }

        private void deleteOrderMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                DeleteOrderAction(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
            
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadedForm(null, null);
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void importMenuItem_Click(object sender, EventArgs e)
        {
            SelectImportDataAction(null, null);
        }

        private void exportMenuItem_Click(object sender, EventArgs e)
        {
            SelectExportDataAction(null, null);
        }
    }
}